package editorsvg.editor.vista;

import editorsvg.editor.documento.ElementoEditable;
import editorsvg.editor.documento.Simbolo;
import editorsvg.editor.herramientas.HerramientaSimbolo;
import editorsvg.gui.generico.ModeloLista;
import javax.swing.JOptionPane;

/**
 *
 * @author  jbp
 */
public class PanelSimbolos extends javax.swing.JPanel {

    private EditorDocumento editor;
    private ModeloLista modeloLista;
    private ElementoEditable editadoAnterior;

    /** Creates new form PanelSimbolos */
    public PanelSimbolos() {
        initComponents();
    }

    public void setEditor(EditorDocumento editor) {
        this.editor = editor;
        modeloLista = new ModeloLista(editor.getSimbolos().getLista());
        listaSimbolos.setModel(modeloLista);
    }

    public void actualizar() {
        modeloLista.actualizar();
    }

    public Simbolo getSimboloSeleccionado() {
        Object seleccionado = listaSimbolos.getSelectedValue();
        if (seleccionado instanceof Simbolo) {
            return (Simbolo) seleccionado;
        } else {
            return null;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listaSimbolos = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        botonInsertarSimbolo = new javax.swing.JButton();
        botonEditar = new javax.swing.JButton();
        botonFinEdicion = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        listaSimbolos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(listaSimbolos);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        botonInsertarSimbolo.setText("insertar");
        botonInsertarSimbolo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonInsertarSimboloActionPerformed(evt);
            }
        });

        botonEditar.setText("editar");
        botonEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEditarActionPerformed(evt);
            }
        });

        botonFinEdicion.setText("x");
        botonFinEdicion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonFinEdicionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botonFinEdicion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonEditar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonInsertarSimbolo)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonFinEdicion)
                    .addComponent(botonEditar)
                    .addComponent(botonInsertarSimbolo))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents

private void botonInsertarSimboloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonInsertarSimboloActionPerformed

    Simbolo simbolo = getSimboloSeleccionado();
    
    ElementoEditable elemento = editor.getDocumento();
    if (elemento == simbolo || simbolo.contieneElemento(elemento)) {
        JOptionPane.showMessageDialog(editor, "No se puede colocar un símbolo dentro de sí mismo");
        return;
    }
    
    if (simbolo != null) {
        editor.setHerramienta(new HerramientaSimbolo(editor, simbolo));
    }  
    
}//GEN-LAST:event_botonInsertarSimboloActionPerformed

private void botonEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEditarActionPerformed
    
    Simbolo simbolo = getSimboloSeleccionado();
    if (simbolo != null) {
        
        editadoAnterior = editor.getDocumento();
        editor.setDocumento(simbolo);
    }
    
}//GEN-LAST:event_botonEditarActionPerformed

private void botonFinEdicionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonFinEdicionActionPerformed
    
    if (editadoAnterior != null) {
        editor.setDocumento(editadoAnterior);
        editadoAnterior = null;
    }
    
}//GEN-LAST:event_botonFinEdicionActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonEditar;
    private javax.swing.JButton botonFinEdicion;
    private javax.swing.JButton botonInsertarSimbolo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listaSimbolos;
    // End of variables declaration//GEN-END:variables
}
